package common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import modules.configuration.GameConf;

public class SQLMgr {

    static private ExecutorService Exec;
    static SQLMgr _self;
    private static Connection DB;
    private static ResultSet RS;
    private static int RSU;
    private static Statement stat;

    public SQLMgr(byte n) {
        if (_self != null) {
            return;//Only 1 Instance
        }
        _self = this;
        Exec = Executors.newFixedThreadPool(n);//n threads		
    }

    public static boolean setSQLConnection() {
        try {
            DB = DriverManager.getConnection("jdbc:mysql://" + GameConf._dbHost + "/" + GameConf._dbName, GameConf._dbUser, GameConf._dbPass);
            if (DB.isClosed()) {
                return false;
            } else {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static void closeSQLConnection(Connection con) {
        try {
            if (!DB.isClosed()) {
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Statement createStat() {
        try {
            // Create a new statement for a query
            stat = getSQLConnection().createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return stat;
    }

    public static ResultSet query(Statement pStat, String pQuery) {
        try {
            if (!getSQLStatut()) {
                GameServer.writeLine("SQL Error : Unable to execute the query - " + pQuery);
                RS = null;
            } else {
                // Get results of the query
                RS = pStat.executeQuery(pQuery);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return RS;
    }

    public static int queryU(Statement pStat, String pQuery) {
        try {
            if (!getSQLStatut()) {
                GameServer.writeLine("SQL Error : Unable to execute the query - " + pQuery);
                RSU = -1;
            } else {
                RSU = pStat.executeUpdate(pQuery);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return RSU;
    }

    public static void freeSQLQuery(ResultSet oQuery) {
        try {
            oQuery.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void freeSQLStat(Statement oStat) {
        try {
            oStat.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static boolean onInitTest() {
        try {
            DB = DriverManager.getConnection("jdbc:mysql://" + GameConf._dbHost + "/" + GameConf._dbName, GameConf._dbUser, GameConf._dbPass);
            if (DB.isClosed()) {
                return false;
            }
            DB.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Object getSelfSQL() {
        return _self;
    }

    public static Connection getSQLConnection() {
        return DB;
    }

    public static ExecutorService getExec() {
        return Exec;
    }

    public static boolean getSQLStatut() {
        try {
            if (DB.isClosed()) {
                return false;
            } else {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
